package com.flying.carclub.utils;

import com.flying.carclub.bean.UserInfoBean;
import com.flying.carclub.modules.home.mvp.ArticleDailyModel;
import com.flying.carclub.modules.home.mvp.ArticleModel;
import com.flying.carclub.modules.home.mvp.TopArticleModel;
import com.stonesun.newssdk.bean.UserInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuxiaocheng on 2018/1/12.
 */

public class ResponseParser {

    public static boolean isRequestSuccess(String strData) {
        boolean bResult = false;
        try {
            JSONObject obj = new JSONObject(strData);
            if (obj.has("code")) {
                if (obj.optInt("code") == 0) {
                    bResult = true;
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return bResult;
    }

    public static String parseMessage(String strData) {
        String res = null;
        try {
            JSONObject obj = new JSONObject(strData);
            if (obj.has("message")) {
                res = obj.optString("message");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }


    static String article = "{\"date\":\"20180115\",\"stories\":[{\"images\":[\"https:\\/\\/pic3.zhimg.com\\/v2-58ebc0fb7b9d1d25745b653be7595842.jpg\"],\"type\":0,\"id\":9665338,\"ga_prefix\":\"011511\",\"title\":\"开着自己改造的房车，我们俩在这 12 平米里吃饭、睡觉、旅行\"},{\"images\":[\"https:\\/\\/pic2.zhimg.com\\/v2-508a1b819fa7032db9cca86f73c56d39.jpg\"],\"type\":0,\"id\":9665171,\"ga_prefix\":\"011509\",\"title\":\"「最赚钱的公司都有哪些共性？」这么问出来你就已经错了\"},{\"images\":[\"https:\\/\\/pic4.zhimg.com\\/v2-3b73e8a0fca1bb0ebd2b49129c2273f7.jpg\"],\"type\":0,\"id\":9665608,\"ga_prefix\":\"011508\",\"title\":\"别想着流感高发季，感冒药就能随便吃，小心伤了你的肝\"},{\"images\":[\"https:\\/\\/pic3.zhimg.com\\/v2-23e42514bb619b43eec2f3a3379e1df6.jpg\"],\"type\":0,\"id\":9663965,\"ga_prefix\":\"011507\",\"title\":\"干我们这行的，毕业想拿知名互联网公司 offer 得讲求方法\"},{\"images\":[\"https:\\/\\/pic3.zhimg.com\\/v2-b948a90b0e0678eb2efdf4875af1c6ce.jpg\"],\"type\":0,\"id\":9665784,\"ga_prefix\":\"011507\",\"title\":\"为什么东北人这么爱穿貂？\"},{\"images\":[\"https:\\/\\/pic4.zhimg.com\\/v2-6d3d7e0951dfd7799f59bf9be5d4e1a3.jpg\"],\"type\":0,\"id\":9665448,\"ga_prefix\":\"011507\",\"title\":\"星战、芭比娃娃和阴盛阳衰的开发商：非凡公主希瑞的背后事\"},{\"images\":[\"https:\\/\\/pic2.zhimg.com\\/v2-68faac668c54238ef299930e9481fe69.jpg\"],\"type\":0,\"id\":9665779,\"ga_prefix\":\"011506\",\"title\":\"瞎扯 · 如何正确地吐槽\"}],\"top_stories\":[{\"image\":\"https:\\/\\/pic4.zhimg.com\\/v2-26e1df9e61b32730328ab822ad5a792f.jpg\",\"type\":0,\"id\":9665338,\"ga_prefix\":\"011511\",\"title\":\"开着自己改造的房车，我们俩在这 12 平米里吃饭、睡觉、旅行\"},{\"image\":\"https:\\/\\/pic3.zhimg.com\\/v2-089d7022491bcd2ea34fe04458172e76.jpg\",\"type\":0,\"id\":9665608,\"ga_prefix\":\"011508\",\"title\":\"别想着流感高发季，感冒药就能随便吃，小心伤了你的肝\"},{\"image\":\"https:\\/\\/pic3.zhimg.com\\/v2-eaba192f0324019c43e1d7619b83498e.jpg\",\"type\":0,\"id\":9665448,\"ga_prefix\":\"011507\",\"title\":\"星战、芭比娃娃和阴盛阳衰的开发商：非凡公主希瑞的背后事\"},{\"image\":\"https:\\/\\/pic4.zhimg.com\\/v2-e798fead558776a633e3d4131cec68cb.jpg\",\"type\":0,\"id\":9663965,\"ga_prefix\":\"011507\",\"title\":\"干我们这行的，毕业想拿知名互联网公司 offer 得讲求方法\"},{\"image\":\"https:\\/\\/pic3.zhimg.com\\/v2-d663b336054b8d0da5e4e1992f101d26.jpg\",\"type\":0,\"id\":9665768,\"ga_prefix\":\"011407\",\"title\":\"这是我亲身经历过最尴尬的事……\"}]}";

    public static ArticleDailyModel getArticleList() {
        ArticleDailyModel articleDailyModel = null;
        try {
            JSONObject jsonObject = new JSONObject(article);
            if (jsonObject != null) {
                articleDailyModel = new ArticleDailyModel();
                articleDailyModel.setDate(jsonObject.optString("date"));
                if (jsonObject.has("top_stories")) {

                    JSONArray topArray = jsonObject.optJSONArray("top_stories");
                    if (topArray != null) {
                        ArrayList<TopArticleModel> topArticleModels = new ArrayList<>();
                        for (int i = 0; i < topArray.length(); i++) {
                            JSONObject topObj = topArray.optJSONObject(i);
                            TopArticleModel topArticleModel = new TopArticleModel();

                            topArticleModel.setId(topObj.optInt("id"));
                            topArticleModel.setType(topObj.optInt("type"));
                            topArticleModel.setImage(topObj.optString("image"));
                            topArticleModel.setTitle(topObj.optString("title"));
                            topArticleModel.setGaPrefix(topObj.optString("ga_prefix"));
                            topArticleModels.add(topArticleModel);
                        }
                        articleDailyModel.setTopArticleArrayList(topArticleModels);
                    }
                }
                if (jsonObject.has("stories")) {

                    JSONArray articleArray = jsonObject.optJSONArray("stories");
                    if (articleArray != null) {
                        ArrayList<ArticleModel> articleModels = new ArrayList<>();
                        for (int i = 0; i < articleArray.length(); i++) {
                            JSONObject articleObj = articleArray.optJSONObject(i);
                            ArticleModel articleModel = new ArticleModel();

                            articleModel.setId(articleObj.optInt("id"));
                            articleModel.setType(articleObj.optInt("type"));
                            JSONArray imgArray = articleObj.optJSONArray("images");
                            List<String> imgList = new ArrayList<>();
                            if (imgArray != null) {
                                for (int j = 0; j < imgArray.length(); j++) {
                                    imgList.add(imgArray.optString(j));
                                }
                            }
                            articleModel.setImages(imgList);
                            articleModel.setTitle(articleObj.optString("title"));
                            articleModel.setGaPrefix(articleObj.optString("ga_prefix"));
                            articleModels.add(articleModel);
                        }
                        articleDailyModel.setArticleModelList(articleModels);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return articleDailyModel;
    }

    public static UserInfoBean parseUserInfo(String jsonStr) {
        UserInfoBean userInfoBean = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);

            JSONObject dataObj = jsonObject.optJSONObject("data");
            if (dataObj != null){
                userInfoBean = new UserInfoBean();
                userInfoBean.username = dataObj.optString("username");
                userInfoBean.nickname = dataObj.optString("nickname");
                userInfoBean.realName = dataObj.optString("realname");
                userInfoBean.authKey = dataObj.optString("auth_key");
                userInfoBean.sex = dataObj.optInt("sex");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return userInfoBean;
    }
}
